// Xampler, a MIDI-driven sampler
// Copyright (C) 2006 Nick Thomas
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA

#include <gtkmm/main.h>
#include <gtkmm/messagedialog.h>
#include "Engine.hpp"
#include "MainWindow.hpp"
#include "State.hpp"
#include "SynthMessage.hpp"
using namespace xampler;

int main(int argc, char **argv)
{
    Glib::ustring error_message;
    bool err = false;

    Gtk::Main kit(argc, argv);
    g_state = new State;

    // Initialize the synth message buffer.
    g_synth_message_buffer = new MessageBuffer<SynthMessage>;
    if (!g_synth_message_buffer->init(SYNTH_MESSAGE_BUFFER_SIZE,
                                      error_message)) {
        err = true;
    }

    // Initialize and start the synthesis engine.
    Engine engine;
    if (!err) {
        err = !engine.init(error_message);
    }

    // Report any errors.
    if (err) {
        Gtk::MessageDialog error_dialog("Failure initializing audio engine",
                                        false, Gtk::MESSAGE_ERROR);
        error_dialog.set_secondary_text(error_message);
        error_dialog.run();
    }

    // Create the GUI and start the main loop.
    if (!err) {
        // We allocate this object on the heap because it's extremely
        // large, and we sometimes run out of stack when we allocate
        // it on the stack.
        MainWindow *mainWindow = new MainWindow;
        Gtk::Main::run(*mainWindow);
        delete mainWindow;
    }

    return 0;
}
